package com.zzl.alligator.management.system.service.impl;

import com.zzl.alligator.management.system.domain.SysUser;
import com.zzl.alligator.management.system.service.ISysMenuService;
import com.zzl.alligator.management.system.service.ISysPermissionService;
import com.zzl.alligator.management.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * TODO
 *
 * @author zhouziliang
 * @since 2020/6/26
 */
@Service
public class SysPermissionService implements ISysPermissionService {

    @Autowired
    ISysRoleService roleService;

    @Autowired
    private ISysMenuService menuService;
    /**
     * 根据用户Id获取用户所有的角色信息
     *
     * @param user 用户
     * @return 角色列表
     */
    @Override
    public Set<String> getRolePermission(SysUser user) {
        Set<String> roles = new HashSet<>();
        if (user.isAdmin()) {
            roles.add("admin");
        } else {
            roles.addAll(roleService.findRolePermissionByUserId(user.getUserId()));
        }
        return roles;
    }

    /**
     * 根据用户Id获取用户菜单信息
     *
     * @param user 用户
     * @return 菜单列表
     */
    @Override
    public Set<String> getMenuPermission(SysUser user) {
        Set<String> menus = new HashSet<>();
        if(user.isAdmin()) {
            menus.add("*:*:*");
        } else {
            menus.addAll(menuService.findMenuPermsByUserId(user.getUserId()));
        }
        return menus;
    }
}
